home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11880 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.0 KB  |  83 lines

  1. Newsgroups: comp.lang.c++
  2. Path: king.mcs.drexel.edu!ucphinni
  3. From: ucphinni@mcs.drexel.edu (C. K. Phinnizee)
  4. Subject: Implicit Typecasting
  5. Message-ID: <1996Mar16.203628.15187@mcs.drexel.edu>
  6. Sender: news@mcs.drexel.edu
  7. Organization: Drexel University, Dept. of Math. and Comp. Sci.
  8. X-Newsreader: TIN [version 1.2 PL2]
  9. Date: Sat, 16 Mar 96 20:36:28 GMT
  10.  
  11.     I am trying to write a personalized C++ library using Codewarrior on the
  12. Mac platform (the type of platform should not matter.  My main concern is
  13. portability, followed closely by speed.).
  14.     The library is going to have all of the basic structures such as a
  15. linked list node, different types of stacks, different types of trees,
  16. ...etc... and some higher order classes, like scheduling algorithms. I
  17. have had some success, but there is one nagging question that has been
  18. bothering me.
  19.     In many of my classes, I use inheritence and must change the interface
  20. so the client need not worry about the implementation details (standard c++
  21. practice) about the interface.    For example, in one part of my library, I
  22. have something like this:
  23.  
  24. class llNode {
  25.     typedef llNode ll;
  26.     public:
  27.         llNode(ll * anext = NULL) { nextptr = anext;}
  28.         ll * next(void) { return nextptr;}
  29.         ll * next(ll * anext) { return nextptr = anext;}
  30.         ... cut code ...
  31.     private:
  32.         ll * nextptr;
  33. };
  34.  
  35. class dlNode : public llNode {
  36.     typedef dlNode dl;
  37.     public:
  38.         dlNode(dl * aprev = NULL,dl * anext = NULL) : ll((ll *) anext)
  39.         { prevptr = aprev;}
  40.         dl * prev(void) { return (dl *) prevptr.next();}
  41.         dl * prev(dl * aprev) { return (dl *) prevptr.next((dl *) aprev);}
  42.         dl * next(void) { return (dl *) ll::next();}
  43.         dl * next(dl * anext) { return (dl *) ll::next((ll *) anext);}
  44.         }
  45.     private:
  46.         ll prevptr;
  47. };
  48.  
  49.     My question is of a readability more than a functionality concern.    For
  50. the above code, it would seem to me that a lot of meaningless code (just
  51. typecasting) is being done.     Is there any standard way to eliminate this.
  52. In other words, is there a way for the compiler to automatically equate the
  53. current class to the superclass and compile it without any warnings (i.e.
  54. compiler would replace ll for dl in the abovemented case)  In the previous
  55. case, this would elimate all of the public member functions in the dlNode.
  56.  
  57.     Thank you in advance for your response.
  58.  
  59. P.S.  I once did something similar to what I was asking but it was not
  60. portable.  I have since forgoten how I did it but I believe it went
  61. something like this.
  62.  
  63. class dlNode : public llNode {
  64.   ... stuff ...
  65.   public:
  66.     operator llNode(); 
  67.       // I am not sure whether you have to protype the
  68.       // operator with anything.  If you have to are it is
  69.       // left as an option. Please let me know.
  70. }
  71.  
  72. but when I tried this at home, it did not work.     I am not for certain, but
  73. I believe this works with g++ or something like it.
  74.  
  75.     Is there something more that I have to provide for this to work, or is
  76. this a special feature of the compiler. (Anything for gnu tells to sport
  77. fancy features and this might be one of them)
  78.  
  79.  
  80.  
  81. -- 
  82. "Skool iz kool..."
  83.